package com.linkoog.devtools.json.file_editer.editer.toolbar;

import com.intellij.codeInsight.folding.impl.actions.BaseFoldingHandler;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.FoldRegion;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class CollapseAllRegionsAction extends EditorAction {

    public CollapseAllRegionsAction() {
        super(new BaseFoldingHandler() {
            @Override
            public void doExecute(@NotNull final Editor editor, @Nullable Caret caret, DataContext dataContext) {
                final List<FoldRegion> regions = getFoldRegionsForSelection(editor, caret);
                editor.getFoldingModel().runBatchFoldingOperation(() -> {
                    for (FoldRegion region : regions) {
                        region.setExpanded(false);
                    }
                });
            }
        });
    }

}
